package org.laptech.calculator;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.os.Bundle;
import android.text.Html;
import android.text.Spanned;
import android.util.Log;
import android.util.Pair;
import android.util.TypedValue;
import android.view.ContextMenu;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SeekBar;
import android.widget.Spinner;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;

import org.laptech.calc.R;
import org.laptech.calculator.logic.Game;
import org.laptech.calculator.logic.Hero;
import org.laptech.calculator.logic.Spell;
import org.laptech.calculator.logic.Talent;
import org.laptech.calculator.logic.TalentCalculator;
import org.laptech.calculator.utils.Utils;

import java.util.ArrayList;


public class MainActivity extends Activity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        setContentView(R.layout.main);
        initPopupMenu();
        initTalentCalculator();


        // registerForContextMenu(findViewById(R.id.imageView));
    }


    //Иницилизация меню появл слева
    private void initPopupMenu() {
        final SlidingMenu menu = new SlidingMenu(this);

        menu.setMode(SlidingMenu.LEFT);
        menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
        menu.setFadeDegree(0.35f);
        menu.attachToActivity(this, SlidingMenu.SLIDING_WINDOW);
        menu.setMenu(R.layout.sidemenu);
        menu.setBehindWidthRes(R.dimen.slidingmenu_behind_width);

        String[] items = {getResources().getString(R.string.choosehero), getResources().getString(R.string.about), getResources().getString(R.string.spells), getResources().getString(R.string.talents)};
        ((ListView) findViewById(R.id.sidemenu)).setAdapter(
                new ArrayAdapter<Object>(
                        this,
                        R.layout.sidemenuitem,
                        R.id.text,
                        items
                )
        );
        final Context context = this;
                ((ListView) findViewById(R.id.sidemenu)).setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                for (int i = 0; i < parent.getChildCount(); i++) {
                    TextView tv = (TextView) ((ViewGroup) parent.getChildAt(i)).getChildAt(0);
                    tv.setBackgroundResource(R.drawable.menuitembg);
                    tv.setTextColor(Color.WHITE);

                }
                TextView tv = (TextView) ((ViewGroup) view).getChildAt(0);
                tv.setTextColor(Color.parseColor("#ee0000"));
                ((ViewGroup) view).getChildAt(0).setBackgroundResource(R.drawable.selmenuitembg);
                switch ((int) id) {
                    case 0:
                        selectHeroLayoutInit();
                        break;
                    case 1:
                        infoHeroLayoutInit();
                        String[] items2 = {getResources().getString(R.string.choosehero), getResources().getString(R.string.spells), getResources().getString(R.string.talents)};
                        ((ListView) findViewById(R.id.sidemenu)).setAdapter(
                                new ArrayAdapter<Object>(
                                        context,
                                        R.layout.sidemenuitem,
                                        R.id.text,
                                        items2
                                )
                        );
                        break;
                    case 2:

                        if (Utils.curHero != -1) {
                            heroAbilitiesLayoutInit();
                            initAbilityLayoutListeners();
                        } else {
                            Toast.makeText(context, "Select hero first", Toast.LENGTH_SHORT);
                        }
                        break;
                    case 3:
                        heroTalentsLayoutInit();
                        break;

                }
                menu.showContent();
            }


        });
    }
    // Иницилизация интерфейса информации о талантах героя
    private void heroTalentsLayoutInit() {

    }

    // Иницилизация интерфейса информации о способностях героя
    private void heroAbilitiesLayoutInit() {

            setContentView(R.layout.heroabilities);

            Utils.talentCalculator.initAbilitiesAndAttributes(Utils.curHero); // получаем способности из базы данных
            final Hero hero = Utils.talentCalculator.getHero(Utils.curHero);

            // init hero panel


            ImageView imageView = (ImageView) findViewById(R.id.imageView);
            imageView.setImageResource(getResources().getIdentifier(hero.getName(), "drawable", getPackageName()));

            ImageView gameImage = (ImageView) findViewById(R.id.gameImage);
            gameImage.setImageResource(getResources().getIdentifier("game_" + hero.getGame().name().toLowerCase(), "drawable", getPackageName()));

            TextView heroNameText = (TextView) findViewById(R.id.heroText);
            heroNameText.setText(getResources().getIdentifier(hero.getName() + "_name", "string", getPackageName()));


            TextView roleText = (TextView) findViewById(R.id.roleText);

            roleText.setText(getResources().getIdentifier(hero.getRole().name().toLowerCase(), "string", getPackageName()));

            // init spells

            ViewGroup spellGroup = (ViewGroup) findViewById(R.id.spellLayout);
            spellGroup.removeAllViews();

            TextView spellsText = new TextView(this);
            spellsText.setTextColor(getResources().getColor(R.color.spells_text_color));
            spellsText.setText(getResources().getString(R.string.spellstext));
            spellGroup.addView(spellsText, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));

            final ArrayList<Spell> spells = hero.getSpells();
            for (int i = 0; i < spells.size(); i++) {
                final Spell spell = spells.get(i);
                SpellImageView iv = new SpellImageView(Character.toUpperCase(spell.getHotkey()), this);
                iv.setImageResource(getResources().getIdentifier(spell.getSpellID(), "drawable", getPackageName()));
                int size = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, getResources().getDisplayMetrics());
                spellGroup.addView(iv, new ViewGroup.LayoutParams(size, size));
                final int finalI = i;
                iv.setOnClickListener(new View.OnClickListener() {


                    @Override
                    public void onClick(View v) {
                        selectSpellEvent(v, finalI);

                    }
                });
                if(Utils.selectedSpell==i)
                {
                    final TextView tv = (TextView) findViewById(R.id.spellDescriptionID);

                    tv.setVisibility(View.VISIBLE);
                    iv.setColorFilter(Color.GRAY, PorterDuff.Mode.ADD);

                    initSpellDescription(spell);
                }
            }

    }

    // Иницилизация интерфейса информации о герое
    private void infoHeroLayoutInit() {

    }

    // Иницилизация интерфейса выбора героя
    private void selectHeroLayoutInit() {
        setContentView(R.layout.selecthero);

        Utils.talentCalculator.selectHeroes();
        final Spinner roleSpinner = (Spinner) findViewById(R.id.RoleSpinner);
        final Spinner gameSpinner = (Spinner) findViewById(R.id.GameSpinner);

        String[] str = getResources().getStringArray(R.array.role);
        Pair<String, Integer> pair2[] = new Pair[str.length];
        for (int i = 0; i < str.length; i++) {
            pair2[i] = new Pair<String, Integer>(str[i], 0);
        }
        ExtAdapter roleAdapter = new ExtAdapter(this, R.layout.simple_spinner_item, pair2);
        Pair<String, Integer> pair[] = new Pair[5];
        pair[0] = new Pair<String, Integer>("All Games", 0);
        pair[1] = new Pair<String, Integer>("Warcraft", R.drawable.game_war);
        pair[2] = new Pair<String, Integer>("Diablo", R.drawable.game_diablo);
        pair[3] = new Pair<String, Integer>("Starcraft", R.drawable.game_sc);
        pair[4] = new Pair<String, Integer>("Lost Vikings", R.drawable.game_lost);
        ExtAdapter gameAdapter = new ExtAdapter(this, R.layout.simple_spinner_item, pair);
        roleSpinner.setAdapter(roleAdapter);
        gameSpinner.setAdapter(gameAdapter);
        gameSpinner.setSelection(0);
        roleSpinner.setSelection(0);
        Utils.role = "All Roles";
        Utils.game = "All Games";
        TextView tv = (TextView) findViewById(R.id.currentHeroTxt);
        if(Utils.curHero==-1)
        {
            tv.setText(R.string.noheroes);
            tv.setText(getResources().getString(R.string.selected_hero)+" "+tv.getText());
        }
        else
        {

            tv.setText(getResources().getIdentifier(Utils.talentCalculator.getHero(Utils.curHero).getName()+"_name","string",getPackageName()));
            tv.setText(getResources().getString(R.string.selected_hero)+" "+tv.getText());
        }
        gameSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                Utils.game = ((Pair<String, Integer>) gameSpinner.getSelectedItem()).first;
                filterHeroes(Utils.role, Utils.game);

            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        roleSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                Utils.role = ((Pair<String, Integer>) roleSpinner.getSelectedItem()).first;
                filterHeroes(Utils.role, Utils.game);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }

        });


    }

    private void filterHeroes(String role, String game) {

        TableLayout tl = (TableLayout) findViewById(R.id.heroestable);
        tl.removeAllViews();
        int size = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 60, getResources().getDisplayMetrics());
        int numofCol = tl.getWidth() / (size+10);
        int curCol = 0;
        TableRow tr = null;
        final ArrayList<ImageView> images = new ArrayList<ImageView>();
        int skippedHeroes = 0;
        if (role != null && game != null) {
            for (int i = 0; i < Utils.talentCalculator.hCount(); i++) {
                final Hero hero = Utils.talentCalculator.getHero(i);
                if (!role.equals("All Roles")) {
                    if (!hero.getRole().name().toLowerCase().contains(role.toLowerCase())) {
                        skippedHeroes++;
                        continue;
                    }
                }
                if (!game.equals("All Games")) {
                    if (game.equals("Warcraft")) {

                        if (hero.getGame() != Game.WAR) {
                            skippedHeroes++;
                            continue;
                        }
                    }
                    if (game.equals("Starcraft")) {

                        if (hero.getGame() != Game.SC) {
                            skippedHeroes++;
                            continue;
                        }
                    }
                    if (game.equals("Diablo")) {

                        if (hero.getGame() != Game.DIABLO) {
                            skippedHeroes++;
                            continue;
                        }
                    }
                    if (game.equals("Lost Vikings")) {
                        if (hero.getGame() != Game.LV) {
                            skippedHeroes++;
                            continue;
                        }
                    }
                }
                if(curCol==0) {
                    tr = new TableRow(this);


                }
                final ImageView image = new ImageView(this);


                image.setImageResource(getResources().getIdentifier(hero.getName(), "drawable", getPackageName()));
                image.setLayoutParams(new TableRow.LayoutParams(size, size));
                image.setPadding(10,10,10,10);
                final int heroIndex = i;
                if(i== Utils.curHero-skippedHeroes)
                {
                    image.setBackgroundColor(0xFFCCFFFF);
                }
                final int finalSkippedHeroes = skippedHeroes;
                image.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        setCurrentHero(heroIndex);
                        image.setBackgroundColor(0xFFCCFFFF);

                        for(int k=0;k<images.size();k++)
                        {
                            if(k!=heroIndex- finalSkippedHeroes)
                            {
                                images.get(k).setBackgroundColor(0x00000000);
                            }
                        }
                        TextView tv = (TextView) findViewById(R.id.currentHeroTxt);
                        tv.setText(getResources().getIdentifier(Utils.talentCalculator.getHero(Utils.curHero).getName()+"_name","string",getPackageName()));
                        tv.setText(getResources().getString(R.string.selected_hero)+" "+tv.getText());

                    }
                });
                images.add(image);
                LinearLayout.LayoutParams il = (LinearLayout.LayoutParams) image.getLayoutParams();
                il.gravity = Gravity.LEFT;
                il.rightMargin = 5;
                tr.addView(image);

                curCol++;

                if (curCol == numofCol) {
                    curCol = 0;
                    tl.addView(tr,new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.MATCH_PARENT));

                }

            }
        }
        if(tr!=null)
        {
            tl.addView(tr,new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.MATCH_PARENT));
        }
    }

    private void initAbilityLayoutListeners() {

        SeekBar seekBar = (SeekBar) findViewById(R.id.seekBar);

        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if (Utils.curLevel != progress + 1) {
                    TextView lvlView = (TextView) findViewById(R.id.levelView);

                    lvlView.setText((progress + 1) + "");

                    Utils.curLevel = progress + 1;

                    if (Utils.selectedSpell != -1)
                        initSpellDescription(Utils.talentCalculator.getHero(Utils.curHero).getSpells().get(Utils.selectedSpell));
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
      /*  ImageView iv = (ImageView) findViewById(R.id.attView);
        iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showAttributes();
            }
        });
        for (int i = 0; i < 7; i++) {
            final int resID = getResources().getIdentifier("tier" + (i + 1) + "caption", "id", getPackageName());
            ImageView tv = (ImageView) findViewById(resID);
            final int finalI = i;
            tv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    tierLabelClick(v, finalI);

                }
            });

        }*/
    }

    private void showAttributes() {
        Utils.isShowAttributes = !Utils.isShowAttributes;

        if (Utils.isShowAttributes) {
            // убираем все панели , показываем новую панель
            final LinearLayout ll = (LinearLayout) findViewById(R.id.Abilities);
            Animation anim1 = AnimationUtils.loadAnimation(this, R.anim.hideanim);


            final Animation anim2 = AnimationUtils.loadAnimation(this, R.anim.showanim);
            final TextView tv = (TextView) findViewById(R.id.attributesDescriptionID);
            anim1.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {

                }

                @Override
                public void onAnimationEnd(Animation animation) {

                    ll.setVisibility(View.GONE);
                    tv.setVisibility(View.VISIBLE);
                    tv.startAnimation(anim2);

                }

                @Override
                public void onAnimationRepeat(Animation animation) {

                }
            });
            ll.startAnimation(anim1);

        } else {
            // отображаем панели, убираем область аттрибутов
            final LinearLayout ll = (LinearLayout) findViewById(R.id.Abilities);
            final Animation anim1 = AnimationUtils.loadAnimation(this, R.anim.showanim);
            final Animation anim2 = AnimationUtils.loadAnimation(this, R.anim.hideanim);
            final TextView tv = (TextView) findViewById(R.id.attributesDescriptionID);

            anim2.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {

                }

                @Override
                public void onAnimationEnd(Animation animation) {
                    tv.setVisibility(View.GONE);
                    ll.startAnimation(anim1);
                    ll.setVisibility(View.VISIBLE);

                }

                @Override
                public void onAnimationRepeat(Animation animation) {

                }
            });
            tv.startAnimation(anim2);

        }
    }

    private void tierLabelClick(View v, int finalI) {

        if (Utils.activeTierRow == -1) {
            Utils.isShowTalentDescription = true;
            final ImageView iv = (ImageView) v;

            iv.setImageResource(getResources().getIdentifier("tt" + (finalI + 1) + "c", "drawable", getPackageName()));
            Utils.activeTierRow = finalI;
            for (int i = 6; i >= 0; i--) {
                if (i != finalI) {

                    final int resID = getResources().getIdentifier("t" + (i + 1) + "row", "id", getPackageName());
                    View tv = findViewById(resID);
                    Animation anim = AnimationUtils.loadAnimation(this, R.anim.slidetobottom);

                    if (i == 0 || i == 1 && finalI == 0) {
                        anim.setAnimationListener(new Animation.AnimationListener() {
                            @Override
                            public void onAnimationStart(Animation animation) {

                            }

                            @Override
                            public void onAnimationEnd(Animation animation) {
                               /* LinearLayout layout = (LinearLayout) findViewById(R.id.talentsLayout);
                                layout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));*/
                                TextView tv2 = (TextView) findViewById(R.id.talentDescriptionID);

                                tv2.setVisibility(View.VISIBLE);


                            }

                            @Override
                            public void onAnimationRepeat(Animation animation) {

                            }
                        });
                    }
                    tv.startAnimation(anim);
                    tv.setVisibility(View.GONE);
                }

            }


        } else {
            Utils.isShowTalentDescription = false;
            Utils.activeTierRow = -1;
            final ImageView iv = (ImageView) v;

            iv.setImageResource(getResources().getIdentifier("tt" + (finalI + 1), "drawable", getPackageName()));
            for (int i = 0; i < 7; i++) {
                if (i != finalI) {
                    final int resID = getResources().getIdentifier("t" + (i + 1) + "row", "id", getPackageName());
                    View tv = findViewById(resID);
                    Animation anim = AnimationUtils.loadAnimation(this, R.anim.restorefrombottom);


                    tv.startAnimation(anim);
                    tv.setVisibility(View.VISIBLE);
                }

            }
            TextView tv2 = (TextView) findViewById(R.id.talentDescriptionID);

            tv2.setVisibility(View.GONE);
        }
    }

    private void initTalentCalculator() {
        Utils.talentCalculator = new TalentCalculator(this);
        setCurrentHero(1); // временно

    }

    private void setCurrentHero(int curHero) {
        if(curHero!=-1 && Utils.prevHero!=Utils.curHero) {
            Utils.prevHero = Utils.curHero;
            Utils.selectedSpell = -1;
        }
        Utils.curHero = curHero;

    }

    private void initHeroInfo(int heroInfo)
    {

        final Hero hero = Utils.talentCalculator.getHero(Utils.curHero);
        Utils.curHero = Utils.curHero;
        // init hero panel
        ViewGroup viewGroup = (ViewGroup) findViewById(R.id.heroInfoLayout);


        ImageView imageView = (ImageView) findViewById(R.id.imageView);
        imageView.setImageResource(getResources().getIdentifier(hero.getName(), "drawable", getPackageName()));

        ImageView gameImage = (ImageView) findViewById(R.id.gameImage);
        gameImage.setImageResource(getResources().getIdentifier("game_" + hero.getGame().name().toLowerCase(), "drawable", getPackageName()));

        TextView heroNameText = (TextView) findViewById(R.id.heroText);
        heroNameText.setText(getResources().getIdentifier(hero.getName() + "_name", "string", getPackageName()));


        TextView roleText = (TextView) findViewById(R.id.roleText);

        roleText.setText(getResources().getIdentifier(hero.getRole().name().toLowerCase(), "string", getPackageName()));

        // init spells

        ViewGroup spellGroup = (ViewGroup) findViewById(R.id.spellLayout);
        spellGroup.removeAllViews();

        TextView spellsText = new TextView(this);
        spellsText.setTextColor(getResources().getColor(R.color.spells_text_color));
        spellsText.setText(getResources().getString(R.string.spellstext));
        spellGroup.addView(spellsText, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));

        final ArrayList<Spell> spells = hero.getSpells();
        for (int i = 0; i < spells.size(); i++) {
            final Spell spell = spells.get(i);
            SpellImageView iv = new SpellImageView(Character.toUpperCase(spell.getHotkey()), this);
            iv.setImageResource(getResources().getIdentifier(spell.getSpellID(), "drawable", getPackageName()));
            int size = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, getResources().getDisplayMetrics());
            spellGroup.addView(iv, new ViewGroup.LayoutParams(size, size));
            final int finalI = i;
            iv.setOnClickListener(new View.OnClickListener() {


                @Override
                public void onClick(View v) {
                    selectSpellEvent(v, finalI);

                }
            });
        }

        // init talents


        for (int i = 0; i < 7; i++) {
            ViewGroup talentsGroup = (ViewGroup) findViewById(getResources().getIdentifier("tier" + (i + 1), "id", getPackageName()));
            talentsGroup.removeAllViews();
        }


        final ArrayList<Talent> talents = hero.getTalents();
        int size = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, getResources().getDisplayMetrics());
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(size, size);
        lp.setMargins(0, 0, 15, 0);
        for (int i = 0; i < talents.size(); i++) {

            final Talent talent = talents.get(i);
            int tier = talent.getTier();
            ViewGroup talentsGroup = (ViewGroup) findViewById(getResources().getIdentifier("tier" + tier, "id", getPackageName()));
            TalentImageView iv = new TalentImageView(this, talent);
            iv.setImageResource(getResources().getIdentifier(talent.getImage(), "drawable", getPackageName()));

            talentsGroup.addView(iv, lp);
            final int finalI = i;


            iv.setOnClickListener(new View.OnClickListener() {


                @Override
                public void onClick(View v) {
                    selectTalentEvent(v, finalI);


                }
            });

        }
        // init attributes
        initAttributes();

    }
    /**
     * Событие происходящее при выборе заклинания
     *
     * @param v
     * @param spellIndex
     */
    private void selectSpellEvent(final View v, int spellIndex) {
        ImageView imgView = (ImageView) v;
        final Spell spell = Utils.talentCalculator.getHero(Utils.curHero).getSpells().get(spellIndex);
        ViewGroup parent = (ViewGroup) v.getParent();

        for (int i = 0; i < parent.getChildCount(); i++) {
            if (parent.getChildAt(i) instanceof ImageView) {
                ((ImageView) parent.getChildAt(i)).clearColorFilter();

            }
        }
        if (Utils.selectedSpell != spellIndex) {
            if (Utils.selectedSpell == -1) {
                final TextView tv = (TextView) findViewById(R.id.spellDescriptionID);
                final Animation anim1 = AnimationUtils.loadAnimation(this, R.anim.showanim);




                tv.startAnimation(anim1);

                tv.setVisibility(View.VISIBLE);


            }
            Utils.selectedSpell = spellIndex;

            imgView.setColorFilter(Color.GRAY, PorterDuff.Mode.ADD);

            initSpellDescription(spell);


        } else {
            Utils.selectedSpell = -1;
            imgView.clearColorFilter();
            final Animation anim1 = AnimationUtils.loadAnimation(this, R.anim.restorefromright);
            Animation anim2 = AnimationUtils.loadAnimation(this, R.anim.hideanim);

            final TextView tv = (TextView) findViewById(R.id.spellDescriptionID);

            tv.startAnimation(anim2);

            tv.setVisibility(View.GONE);

        }

    }
    private void initAttributes() {
        Hero hero = Utils.talentCalculator.getHero(Utils.curHero);
        TextView attV = (TextView) findViewById(R.id.attributesDescriptionID);
        StringBuilder strb = new StringBuilder();
        strb.append("<font color=\"#ffffff\"><big>");
        strb.append(getResources().getString(R.string.health) + ": ");
        strb.append("</big></font>");
        strb.append("<font color=\"#B20000\">");
        strb.append(hero.getAttributes().getHp() + Utils.curLevel * hero.getAttributes().getHpplvl());
        strb.append("</font>");
        strb.append("<br>");
        strb.append("<font color=\"#ffffff\"><big>");
        strb.append(getResources().getString(R.string.hpregen) + ": ");
        strb.append("</big></font>");
        strb.append("<font color=\"#B20000\">");
        strb.append(String.format("%8.2f", hero.getAttributes().getHpreg() + Utils.curLevel * hero.getAttributes().getHpregplvl()));
        strb.append("</font>");
        strb.append("<br>");
        switch (hero.getAttributes().getResource()) {
            case MANA:
                strb.append("<font color=\"#ffffff\"><big>");
                strb.append(getResources().getString(R.string.mana) + ": ");
                strb.append("</big></font>");
                strb.append("<font color=\"#0000F6\">");
                strb.append(hero.getAttributes().getRes() + Utils.curLevel * hero.getAttributes().getResplvl());
                strb.append("</font>");
                strb.append("<br>");
                strb.append("<font color=\"#ffffff\"><big>");
                strb.append(getResources().getString(R.string.manaregen) + ": ");
                strb.append("</big></font>");
                strb.append("<font color=\"#0000F6\">");
                strb.append(String.format("%8.2f", hero.getAttributes().getResreg() + Utils.curLevel * hero.getAttributes().getResregplvl()));
                strb.append("</font>");
                break;
            case FURY:
                strb.append("<font color=\"#ffffff\"><big>");
                strb.append(getResources().getString(R.string.fury) + ": ");
                strb.append("</big></font>");
                strb.append("<font color=\"#FF4719\">");
                strb.append(hero.getAttributes().getRes() + Utils.curLevel * hero.getAttributes().getResplvl());
                strb.append("</font>");
                break;
            case BREW:
                strb.append("<font color=\"#ffffff\"><big>");
                strb.append(getResources().getString(R.string.brew) + ": ");
                strb.append("</big></font>");
                strb.append("<font color=\"#CC9900\">");
                strb.append(hero.getAttributes().getRes() + Utils.curLevel * hero.getAttributes().getResplvl());
                strb.append("</font>");
            default:
                strb.append("<font color=\"#ffffff\"><big>");
                strb.append(getResources().getString(R.string.energy) + ": ");
                strb.append("</big></font>");
                strb.append("<font color=\"#CC9900\">");
                strb.append(getResources().getString(R.string.nan));
                strb.append("</font>");


        }
        strb.append("<br>");
        strb.append("<font color=\"#ffffff\"><big>");
        strb.append(getResources().getString(R.string.as) + ": ");
        strb.append("</big></font>");
        strb.append("<font color=\"#005700\">");
        strb.append(String.format("%8.2f", hero.getAttributes().getAs()));
        strb.append("</font>");
        strb.append("<br>");
        strb.append("<font color=\"#ffffff\"><big>");
        strb.append(getResources().getString(R.string.dmg) + ": ");
        strb.append("</big></font>");
        strb.append("<font color=\"#FF6666\">");
        strb.append(hero.getAttributes().getDmg() + Utils.curLevel * hero.getAttributes().getDmgplvl());
        strb.append("</font>");
        Spanned spanned = Html.fromHtml(strb.toString());
        attV.setText(spanned);
    }


    /**
     * Событие происходящее при выборе таланта
     *
     * @param v
     * @param talentIndex
     */
    private void selectTalentEvent(View v, int talentIndex) {
        ArrayList<Talent> talents = Utils.talentCalculator.getHero(Utils.curHero).getTalents();
        final Talent talent = talents.get(talentIndex);
        Utils.talentCalculator.setSelTalent(talent.getTier(), talent.getCol());

        ViewGroup talentsGroup = (ViewGroup) findViewById(getResources().getIdentifier("tier" + talent.getTier(), "id", getPackageName()));

        for (int i = 0; i < talentsGroup.getChildCount(); i++) {
            TalentImageView tiv = (TalentImageView) talentsGroup.getChildAt(i);
            if (Utils.talentCalculator.getTierValue(talent.getTier()) == i) {
                tiv.setAlpha(1.0f);

            } else {
                tiv.setAlpha(0.3f);
            }
        }
        initTalentDescription();

    }

    private void initTalentDescription() {
        final ArrayList<Talent> talents = Utils.talentCalculator.getHero(Utils.curHero).getTalents();
        Talent talent = null;
        for (int i = 0; i < talents.size(); i++) {
            if (talents.get(i).getTier() == Utils.activeTierRow && talents.get(i).getCol() == Utils.talentCalculator.getTierValue(Utils.activeTierRow)) {
                talent = talents.get(i);
            }
        }
        if (talent != null) {
            TextView tv = (TextView) findViewById(R.id.talentDescriptionID);
            tv.setText(talent.getTalentID());
        }
    }



    private void initSpellDescription(Spell spell) {
        TextView tv = (TextView) findViewById(R.id.spellDescriptionID);


        StringBuilder strb = new StringBuilder();
        strb.append("<font color=\"#ffffff\"><big>" + getResources().getString(getResources().getIdentifier(spell.getSpellID() + "_name", "string", getPackageName())) + "</big></font>");
        strb.append("<br>");

        if (spell.getRescost() != 0) {

            final Hero hero = Utils.talentCalculator.getHero(Utils.curHero);
            switch (hero.getAttributes().getResource()) {
                case FURY:
                    strb.append("<font color=\"#4DDDF0\">" + getResources().getString(R.string.fury) + "</font>: ");

                    break;
                case MANA:
                    strb.append("<font color=\"#4DDDF0\">" + getResources().getString(R.string.mana) + "</font>: ");
                    break;
                case BREW:
                    strb.append("<font color=\"#CC9900\">" + getResources().getString(R.string.brew) + "</font>: ");
                    break;
                case NONE:

                    break;
            }

            strb.append("</font><font color=#CC93C4>" + spell.getRescost() + "</font><font color=#ffffff>");
            strb.append("<br></font>");

        } else
            strb.append("<font color=#ffffff>" + getResources().getString(R.string.nocost) + "<br>");
        if (spell.getCooldown() != 0) {
            strb.append(getResources().getString(R.string.cooldown) + ": ");
            strb.append("</font><font color=#CC93C4>" + spell.getCooldown() + "<br>");
        }
        strb.append("</font>");
        String descr = getResources().getString(getResources().getIdentifier(spell.getSpellID() + "_desc", "string", getPackageName()));
        final Hero hero = Utils.talentCalculator.getHero(Utils.curHero);
        strb.append("<font color=#ffffff>");
        Log.e("lvl", Utils.curLevel+"");
        for (int i = 0; descr.contains("%%"); i++) {
            descr = descr.replaceFirst("%%", "</font><font color=#CC93C4>" + (spell.getParams().get(i).val + spell.getParams().get(i).valplvl * Utils.curLevel) + "</font><font color=#ffffff>");
        }
        strb.append(descr);
        strb.append("</font>");
        Spanned spanned = Html.fromHtml(strb.toString());
        tv.setText(spanned);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.

        getMenuInflater().inflate(R.menu.menu_main, menu);

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }


    public void changeHero(View view) {
        openContextMenu(view);
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
    /*    if(v.getId()==R.id.imageView) {
            super.onCreateContextMenu(menu, v, menuInfo);
            menu.setHeaderTitle("Настройки");

            menu.add("Choose another hero");
            menu.add("Show stats");
        }
        */
    }
}
